import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/utils/icon.dart';
import 'package:star_author/utils/utils.dart';

class SelectModel<T> {
  late T data;
  late bool select;
  late bool enable;

  SelectModel({required this.data, this.select: false, this.enable: true});
}

enum SelectDirection { normal, reverse }
enum SelectMode { multi, single }
enum SelectHeightMode { normal, auto }

class SelectWidget extends StatefulWidget {
  late List<SelectModel> list;
  late Widget Function(SelectModel) itemBuilder;
  late List<Widget> Function(List<SelectModel>)? buttonBuilder;
  late Function(List<SelectModel>) onChange;
  late SelectDirection direction;
  late SelectMode mode;
  late SelectHeightMode heightMode;

  SelectWidget({
    Key? key,
    required this.list,
    required this.itemBuilder,
    this.buttonBuilder,
    required this.onChange,
    this.direction: SelectDirection.normal,
    this.mode: SelectMode.multi,
    this.heightMode: SelectHeightMode.normal,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => SelectWidgetState();
}

class SelectWidgetState extends State<SelectWidget> {
  late Widget Function(SelectModel) itemBuilder = widget.itemBuilder;

  bool checkAllSelected() {
    List enables = widget.list.where((item) => item.enable).toList();
    List temp = enables.where((item) => item.select).toList();
    return temp.length == enables.length;
  }

  List<Widget> renderIcon(SelectModel item) {
    return [
      Visibility(
        child: Container(
          decoration: BoxDecoration(
            color: item.enable ? ColorConf.white : Color(0xFFdedede),
            borderRadius: BorderRadius.circular(10),
          ),
          child: Icon(
            IconFont.yuan,
            size: 20,
            color: Color(0xFF979797),
          ),
        ),
        visible: !item.select,
      ),
      Visibility(
        child: Icon(
          IconFont.gou,
          size: 20,
          color: ColorConf.yellow,
        ),
        visible: item.select,
      ),
    ];
  }

  Widget renderList() {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: widget.list.length,
      padding: EdgeInsets.zero,
      itemBuilder: (BuildContext context, int index) {
        SelectModel item = widget.list[index];
        return InkWell(
          onTap: () {
            if (!widget.list[index].enable) return;
            if (widget.mode == SelectMode.multi)
              setState(() {
                widget.list[index].select = !widget.list[index].select;
                widget.onChange(widget.list);
              });
            else if (widget.mode == SelectMode.single)
              setState(() {
                widget.list.forEach((element) {
                  element.select = false;
                });
                widget.list[index].select = true;
                widget.onChange(widget.list);
              });
          },
          child: Opacity(
            opacity: item.enable ? 1 : 0.8,
            child: Container(
              margin: EdgeInsets.symmetric(vertical: 10),
              child: Row(
                children: [
                  if (widget.direction == SelectDirection.normal) ...[
                    ...renderIcon(item),
                    SizedBox(
                      width: 10,
                    ),
                  ],
                  Expanded(child: itemBuilder(item)),
                  if (widget.direction == SelectDirection.reverse) ...[
                    SizedBox(
                      width: 10,
                    ),
                    ...renderIcon(item),
                  ],
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.heightMode == SelectHeightMode.normal)
      return ConstrainedBox(
        constraints: BoxConstraints(
          maxHeight: Get.height / 2,
        ),
        child: Column(
          children: [
            Expanded(
              child: CupertinoScrollbar(
                child: renderList(),
              ),
            ),
            if (widget.mode == SelectMode.multi)
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    InkWell(
                      onTap: () {
                        if (checkAllSelected()) {
                          widget.list.forEach((item) {
                            if (item.enable) {
                              item.select = false;
                            }
                          });
                        } else {
                          widget.list.forEach((item) {
                            if (item.enable) {
                              item.select = true;
                            }
                          });
                        }
                        setState(() {
                          widget.onChange(widget.list);
                        });
                      },
                      child: Container(
                        height: 50,
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Visibility(
                              child: Icon(
                                IconFont.yuan,
                                size: 20,
                                color: Color(0xFF979797),
                              ),
                              visible: !checkAllSelected(),
                            ),
                            Visibility(
                              child: Icon(
                                IconFont.gou,
                                size: 20,
                                color: ColorConf.yellow,
                              ),
                              visible: checkAllSelected(),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              '全选',
                              style: TextStyle(
                                color: ColorConf.normal,
                                fontSize: 14,
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                    if (checkNotNullOrNotEmpty(widget.buttonBuilder))
                      Container(
                        margin: EdgeInsets.only(
                          left: 10,
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: widget.buttonBuilder!(widget.list),
                        ),
                      )
                  ],
                ),
              )
          ],
        ),
      );
    else if (widget.heightMode == SelectHeightMode.auto)
      return renderList();
    else
      return SizedBox.shrink();
  }
}
